﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class PriceScheme : BasePage
    {
        private static IPolicySchemeBLL policySchemeBLL = IocContainer.Instance.Resolve<IPolicySchemeBLL>();
        protected int PolicyId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["policyId"], out PolicyId);
            if (!IsPostBack)
            {
                IPricePolicyBLL policyBll = IocContainer.Instance.Resolve<IPricePolicyBLL>();
                PricePolicy policy = policyBll.GetPolicyById(PolicyId);
                if (policy != null && !string.IsNullOrEmpty(policy.PolicyName))
                {
                    this.txtPolicyName.Text = "【" + policy.PolicyName + "】 ";
                }
                GridViewBind();
            }
        }

    


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridViewBind();
        }




        private void GridViewBind()
        {
            StringBuilder strWhere = new StringBuilder(" and PolicyId=" + PolicyId);
            if (!string.IsNullOrEmpty(this.txtName.Text))
            {
                strWhere.Append(" and SchemeName like '%" + this.txtName.Text + "%'");
            }
            if (!string.IsNullOrEmpty(this.txtCode.Text))
            {
                strWhere.Append(" and SchemeCode like '%" + this.txtCode.Text + "%'");
            }
            //if (!string.IsNullOrEmpty(PolicyId))
            //{
            //    strWhere.Append(" and PolicyId=" + PolicyId);
            //}

            GridView1.Visible = true;
            DataTable dtProducts = policySchemeBLL.QueryPolicyScheme(strWhere.ToString());
            if (dtProducts != null && dtProducts.Rows.Count > 0)
            {
                this.GridView1.DataSource = dtProducts;
                this.GridView1.DataBind();
            }
            else
            {
                GridView1.Visible = false;
            }

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                RequiredFieldValidator re = new RequiredFieldValidator();   // 创建一个非空验证控件
                re.ControlToValidate = "txtPrice";
                re.ErrorMessage = "不含税单价不能为空";
                re.Display = ValidatorDisplay.Dynamic;
                re.Text = "*";
                e.Row.Cells[3].Controls.Add(re);              //           将该空间添加到GridView控件中
                RegularExpressionValidator rev = new RegularExpressionValidator();
                rev.ControlToValidate = "txtPrice";
                rev.Display = ValidatorDisplay.Dynamic;
                rev.ValidationExpression = @"^\d*$|[1-9]\d*\.\d*|0\.\d*[1-9]\d*$";
                rev.Text = "*";
                rev.ErrorMessage = "不含税单价必须是整数或浮点数";
                e.Row.Cells[3].Controls.Add(rev);

                RequiredFieldValidator re2 = new RequiredFieldValidator();   // 创建一个非空验证控件
                re2.ControlToValidate = "txtTaxRatio";
                re2.ErrorMessage = "税率不能为空";
                re2.Display = ValidatorDisplay.Dynamic;
                re2.Text = "*";
                e.Row.Cells[4].Controls.Add(re2);              //           将该空间添加到GridView控件中
                RegularExpressionValidator rev2 = new RegularExpressionValidator();
                rev2.ControlToValidate = "txtTaxRatio";
                rev2.Display = ValidatorDisplay.Dynamic;
                rev2.ValidationExpression = @"^\d*$|[1-9]\d*\.\d*|0\.\d*[1-9]\d*$";
                rev2.Text = "*";
                rev2.ErrorMessage = "税率必须是整数或浮点数";
                e.Row.Cells[4].Controls.Add(rev2);


                ValidationSummary va = new ValidationSummary();        //    创建一个范围验证控件
                va.ShowSummary = false;
                va.ShowMessageBox = true;
                e.Row.Cells[3].Controls.Add(va);               //    添加到GridView中
                e.Row.Cells[4].Controls.Add(va);

            }

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridViewBind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridViewBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
                string strtxtSchemeCode = ((Label)GridView1.Rows[e.RowIndex].FindControl("txtSchemeCode")).Text;
                string strtxtSchemeName = ((Label)GridView1.Rows[e.RowIndex].FindControl("txtSchemeName")).Text;
                string strddlPriceType = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlPriceType")).SelectedValue;
                string strtxtPrice = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPrice")).Text;
                string strtxtTaxRatio = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTaxRatio")).Text;
                string strtxtTaxPrice = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTaxPrice")).Text;
                //string strtxtUnit = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUnit")).Text;
                string strtxtProductName = ((Label)GridView1.Rows[e.RowIndex].FindControl("txtProductName")).Text;
                string strtxtStartDate = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtStartDate")).Text;
                string strtxtEndDate = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEndDate")).Text;
                string strddlSpecialType = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlSpecialType")).SelectedValue;
                string strhideProductId = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hideProductId")).Value;


                int policyId = 0;
                int.TryParse(Request.QueryString["PolicyId"], out policyId);
                PolicyScheme scheme = new PolicyScheme();
                scheme.SchemeName = strtxtSchemeName;
                scheme.SchemeCode = strtxtSchemeCode;
                scheme.PolicyId = policyId;
                scheme.PriceType = strddlPriceType;
                scheme.Price = Convert.ToDecimal(strtxtPrice);
                scheme.TaxPrice = Convert.ToDecimal(strtxtTaxPrice);
                scheme.Unit = " ";
                scheme.IsSpecialType = strddlSpecialType == "0" ? false : true;
                scheme.StartTime = Convert.ToDateTime(strtxtStartDate);
                scheme.EndTime = Convert.ToDateTime(strtxtEndDate);
                scheme.ProductId = Convert.ToInt32(strhideProductId);
                scheme.TaxRatio = Convert.ToDecimal(strtxtTaxRatio);

                scheme.Id = int.Parse(id);
                policySchemeBLL.Update(scheme);
              

                GridView1.EditIndex = -1;
                GridViewBind();

                Alert("更新成功！");
            }
            catch (Exception ex)
            {
                Logger.Instance.Error(ex);
                Alert("发生异常：\r\n" + ex.Message);
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();

            try
            {
                policySchemeBLL.Delete(int.Parse(id));
              

                GridView1.EditIndex = -1;
                GridViewBind();
                  Alert("删除成功！");
            }
            catch (Exception ex)
            {
                Logger.Instance.Error(ex);
            }


        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridViewBind();
        }



    }
}
